package leju.common.update;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;

import com.leju.common.util.Logger;
import com.loopj.android.http.HttpUtils;
import com.loopj.android.http.JsonHttpResponseHandler;

public class AutoUpdate {
	public Activity activity = null;
	private ApkDownloader apkDownloader;
	OnCheckUpDateListener onCheckUpDateListener;
	public int versionCode = 0;
	public String versionName = "";


	private ProgressDialog dialog;
	private String appName;
	int appid = 0;
	int iconid = 0;
	Version mVersion = null;

	public AutoUpdate(Activity enterActivity, String checkUrl,
			int iconResouceId, String appName,
			OnCheckUpDateListener onCheckUpDateListener) {

		this.activity = enterActivity;
		iconid = iconResouceId;
		this.appName = appName;
		this.onCheckUpDateListener = onCheckUpDateListener;
		getCurrentVersion();

		HttpUtils.get(checkUrl, null, jsonHttpResponseHandler);

	}

	public void showWaitDialog() {
		dialog = new ProgressDialog(activity);
		dialog.setMessage("正在下载更新...");
		dialog.setIndeterminate(true);
		dialog.setCancelable(true);
		dialog.show();
	}

	private void getCurrentVersion() {
		try {
			PackageInfo info = activity.getPackageManager().getPackageInfo(
					activity.getPackageName(), 0);
			this.versionCode = info.versionCode;
			this.versionName = info.versionName;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 开始下载
	 */
	public void startDownLoad() {
		apkDownloader = new ApkDownloader(activity, mVersion.getWebUrl(),
				iconid, appName);
		apkDownloader.downLoadApp();

	}

	JsonHttpResponseHandler jsonHttpResponseHandler = new JsonHttpResponseHandler() {

		@Override
		public void onSuccess(JSONObject jsonObject) {
			Logger.d("jsonObject="+jsonObject.toString());
			try {
				jsonObject = jsonObject.getJSONObject("android");
				mVersion = new Version();
				mVersion.setAppUrl(jsonObject.getString("app_url"));
				String versioncode = jsonObject.getString("latest_version");
				mVersion.setlatestVersionCode(versioncode);
				mVersion.setWebsite(jsonObject.getString("website"));
				mVersion.setWebUrl(jsonObject.getString("web_url"));

				boolean isNeedUpdate = isNeedUpdate(versionName, versioncode);

				onCheckUpDateListener.oncheckResult(isNeedUpdate,mVersion.getWebUrl(), jsonObject.optString("new_function"));

			} catch (JSONException e) {

				e.printStackTrace();
			}
			super.onSuccess(jsonObject);
		}

		@Override
		public void onFailure(Throwable e, JSONObject errorResponse) {
			// TODO Auto-generated method stub
			super.onFailure(e, errorResponse);
		}

	};

	private boolean isNeedUpdate(String curVersionName, String lastestVersionName){
		int compare = curVersionName.compareTo(lastestVersionName);
		if(compare < 0) return true;
		return false;
	}
	
	public interface OnCheckUpDateListener {
		/**
		 * 是否需要更新 新版的更新信息
		 * 
		 * @param isNeedUpdate
		 * @param upDateInfo
		 */
		public void oncheckResult(boolean isNeedUpdate, String webUrl,String upDateInfo);
	}

}